home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb / foo / news-dep.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-01-10  |  15.4 KB  |  640 lines

  1. /* Low level interface to ptrace, for GDB when running under Unix.
  2.    Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. GDB is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GDB is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GDB; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include <stdio.h>
  21. #include "defs.h"
  22. #include "param.h"
  23. #include "frame.h"
  24. #include "inferior.h"
  25.  
  26. #include <sys/param.h>
  27. #include <sys/dir.h>
  28. #include <signal.h>
  29. #include <sys/user.h>
  30. #include <sys/ioctl.h>
  31. #if 0
  32. #include <fcntl.h>
  33. #endif /* I don't think that I need this file.  */
  34.  
  35. #include <a.out.h>
  36. #include <sys/file.h>
  37. #include <sys/stat.h>
  38.  
  39. extern int errno;
  40.  
  41. /* This function simply calls ptrace with the given arguments.  
  42.    It exists so that all calls to ptrace are isolated in this 
  43.    machine-dependent file. */
  44. int
  45. call_ptrace (request, pid, arg3, arg4)
  46.      int request, pid, arg3, arg4;
  47. {
  48.   return ptrace (request, pid, arg3, arg4);
  49. }
  50.  
  51. kill_inferior ()
  52. {
  53.   if (remote_debugging)
  54.     return;
  55.   if (inferior_pid == 0)
  56.     return;
  57.   ptrace (8, inferior_pid, 0, 0);
  58.   wait (0);
  59.   inferior_died ();
  60. }
  61.  
  62. /* This is used when GDB is exiting.  It gives less chance of error.*/
  63.  
  64. kill_inferior_fast ()
  65. {
  66.   if (remote_debugging)
  67.     return;
  68.   if (inferior_pid == 0)
  69.     return;
  70.   ptrace (8, inferior_pid, 0, 0);
  71.   wait (0);
  72. }
  73.  
  74. /* Resume execution of the inferior process.
  75.    If STEP is nonzero, single-step it.
  76.    If SIGNAL is nonzero, give it that signal.  */
  77.  
  78. void
  79. resume (step, signal)
  80.      int step;
  81.      int signal;
  82. {
  83.   errno = 0;
  84.   if (remote_debugging)
  85.     remote_resume (step, signal);
  86.   else
  87.     {
  88.       ptrace (step ? 9 : 7, inferior_pid, 1, signal);
  89.       if (errno)
  90.     perror_with_name ("ptrace");
  91.     }
  92. }
  93.  
  94. void
  95. fetch_inferior_registers ()
  96. {
  97.   register int regno;
  98.   register unsigned int regaddr;
  99.   char buf[MAX_REGISTER_RAW_SIZE];
  100.   register int i;
  101.  
  102.   struct user u;
  103. #ifdef USE_PCB
  104.   unsigned int offset = (char *) &u.u_pcb.pcb_d0 - (char *) &u;
  105. #else
  106.   unsigned int offset = (char *) &u.u_ar0 - (char *) &u;
  107.   offset = ptrace (3, inferior_pid, offset, 0) - KERNEL_U_ADDR;
  108. #endif
  109.  
  110.   for (regno = 0; regno < NUM_REGS; regno++)
  111.     {
  112.       regaddr = register_addr (regno, offset);
  113.       for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
  114.      {
  115.        *(int *) &buf[i] = ptrace (3, inferior_pid, regaddr, 0);
  116.        regaddr += sizeof (int);
  117.      }
  118.       supply_register (regno, buf);
  119.     }
  120. }
  121.  
  122. /* Store our register values back into the inferior.
  123.    If REGNO is -1, do this for all registers.
  124.    Otherwise, REGNO specifies which register (so we can save time).  */
  125.  
  126. store_inferior_registers (regno)
  127.      int regno;
  128. {
  129.   register unsigned int regaddr;
  130.   char buf[80];
  131.   extern char registers[];
  132.  
  133.   struct user u;
  134. #ifdef USE_PCB
  135.   unsigned int offset = (char *) &u.u_pcb.pcb_d0 - (char *) &u;
  136. #else
  137.   unsigned int offset = (char *) &u.u_ar0 - (char *) &u;
  138.   offset = ptrace (3, inferior_pid, offset, 0) - KERNEL_U_ADDR;
  139. #endif
  140.  
  141. #ifdef PTRACE_BUG
  142.   if (regno >= FP0_REGNUM)
  143.       printf ("warning: floating register num %d not written due to OS bug.\n",
  144.           regno);
  145.   else
  146. #endif
  147.   if (regno >= 0)
  148.     {
  149.       int i;
  150.       int *p = (int *) ®isters[REGISTER_BYTE (regno)];
  151.  
  152. #ifdef PTRACE_BUG
  153.       if (regno == FP_REGNUM)
  154.       printf ("warning: ptrace bug for writing register number fp(a6).\n");
  155. #endif
  156.  
  157.       regaddr = register_addr (regno, offset);
  158.       for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
  159.     {
  160.       errno = 0;
  161.       ptrace (6, inferior_pid, regaddr, *p++);
  162.       if (errno != 0)
  163.         {
  164.           sprintf (buf, "writing register number %d[%d]", regno, i);
  165.           perror_with_name (buf);
  166.         }
  167.       regaddr += sizeof (int);
  168.     }
  169.     }
  170.   else
  171.     {
  172. #ifdef PTRACE_BUG
  173.       for (regno = 0; regno < FP0_REGNUM; regno++)
  174. #else
  175.       for (regno = 0; regno < NUM_REGS; regno++)
  176. #endif
  177.     {
  178.       int i;
  179.       int *p = (int *) ®isters[REGISTER_BYTE (regno)];
  180.       regaddr = register_addr (regno, offset);
  181.       for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
  182.         {
  183.           errno = 0;
  184.           ptrace (6, inferior_pid, regaddr, *p++);
  185.           if (errno != 0)
  186.         {
  187.           sprintf (buf, "writing register number %d[%d]", regno, i);
  188.           perror_with_name (buf);
  189.         }
  190.           regaddr += sizeof (int);
  191.         }
  192.     }
  193. #ifdef PTRACE_BUG
  194.       printf ("warning: ptrace bug for writing floating registers(no write).\n");
  195. #endif
  196.     }
  197. }
  198.  
  199. /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
  200.    in the NEW_SUN_PTRACE case.
  201.    It ought to be straightforward.  But it appears that writing did
  202.    not write the data that I specified.  I cannot understand where
  203.    it got the data that it actually did write.  */
  204.  
  205. /* Copy LEN bytes from inferior's memory starting at MEMADDR
  206.    to debugger memory starting at MYADDR. 
  207.    On failure (cannot read from inferior, usually because address is out
  208.    of bounds) returns the value of errno. */
  209.  
  210. int
  211. read_inferior_memory (memaddr, myaddr, len)
  212.      CORE_ADDR memaddr;
  213.      char *myaddr;
  214.      int len;
  215. {
  216.   register int i;
  217.   /* Round starting address down to longword boundary.  */
  218.   register CORE_ADDR addr = memaddr & - sizeof (int);
  219.   /* Round ending address up; get number of longwords that makes.  */
  220.   register int count
  221.     = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
  222.   /* Allocate buffer of that many longwords.  */
  223.   register int *buffer = (int *) alloca (count * sizeof (int));
  224.   extern int errno;
  225.  
  226.   /* Read all the longwords */
  227.   for (i = 0; i < count; i++, addr += sizeof (int))
  228.     {
  229.       errno = 0;
  230.       if (remote_debugging)
  231.     buffer[i] = remote_fetch_word (addr);
  232.       else
  233.     buffer[i] = ptrace (1, inferior_pid, addr, 0);
  234.       if (errno)
  235.     return errno;
  236.     }
  237.  
  238.   /* Copy appropriate bytes out of the buffer.  */
  239.   bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
  240.   return 0;
  241. }
  242.  
  243. /* Copy LEN bytes of data from debugger memory at MYADDR
  244.    to inferior's memory at MEMADDR.
  245.    On failure (cannot write the inferior)
  246.    returns the value of errno.  */
  247.  
  248. int
  249. write_inferior_memory (memaddr, myaddr, len)
  250.      CORE_ADDR memaddr;
  251.      char *myaddr;
  252.      int len;
  253. {
  254.   register int i;
  255.   /* Round starting address down to longword boundary.  */
  256.   register CORE_ADDR addr = memaddr & - sizeof (int);
  257.   /* Round ending address up; get number of longwords that makes.  */
  258.   register int count
  259.     = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
  260.   /* Allocate buffer of that many longwords.  */
  261.   register int *buffer = (int *) alloca (count * sizeof (int));
  262.   extern int errno;
  263.  
  264.   /* Fill start and end extra bytes of buffer with existing memory data.  */
  265.  
  266.   if (remote_debugging)
  267.     buffer[0] = remote_fetch_word (addr);
  268.   else
  269.     buffer[0] = ptrace (1, inferior_pid, addr, 0);
  270.  
  271.   if (count > 1)
  272.     {
  273.       if (remote_debugging)
  274.     buffer[count - 1]
  275.       = remote_fetch_word (addr + (count - 1) * sizeof (int));
  276.       else
  277.     buffer[count - 1]
  278.       = ptrace (1, inferior_pid,
  279.             addr + (count - 1) * sizeof (int), 0);
  280.     }
  281.  
  282.   /* Copy data to be written over corresponding part of buffer */
  283.  
  284.   bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
  285.  
  286.   /* Write the entire buffer.  */
  287.  
  288.   for (i = 0; i < count; i++, addr += sizeof (int))
  289.     {
  290.       errno = 0;
  291.       if (remote_debugging)
  292.     remote_store_word (addr, buffer[i]);
  293.       else
  294.     ptrace (4, inferior_pid, addr, buffer[i]);
  295.       if (errno)
  296.     return errno;
  297.     }
  298.  
  299.   return 0;
  300. }
  301.  
  302. /* Work with core dump and executable files, for GDB. 
  303.    This code would be in core.c if it weren't machine-dependent. */
  304.  
  305. #ifndef N_TXTADDR
  306. #define N_TXTADDR(hdr) 0
  307. #endif /* no N_TXTADDR */
  308.  
  309. #ifndef N_DATADDR
  310. #define N_DATADDR(hdr) hdr.a_text
  311. #endif /* no N_DATADDR */
  312.  
  313. /* Make COFF and non-COFF names for things a little more compatible
  314.    to reduce conditionals later.  */
  315.  
  316. #ifdef COFF_FORMAT
  317. #define a_magic magic
  318. #endif
  319.  
  320. #ifndef COFF_FORMAT
  321. #ifndef AOUTHDR
  322. #define AOUTHDR struct exec
  323. #endif
  324. #endif
  325.  
  326. extern char *sys_siglist[];
  327.  
  328.  
  329. /* Hook for `exec_file_command' command to call.  */
  330.  
  331. extern void (*exec_file_display_hook) ();
  332.    
  333. /* File names of core file and executable file.  */
  334.  
  335. extern char *corefile;
  336. extern char *execfile;
  337.  
  338. /* Descriptors on which core file and executable file are open.
  339.    Note that the execchan is closed when an inferior is created
  340.    and reopened if the inferior dies or is killed.  */
  341.  
  342. extern int corechan;
  343. extern int execchan;
  344.  
  345. /* Last modification time of executable file.
  346.    Also used in source.c to compare against mtime of a source file.  */
  347.  
  348. extern int exec_mtime;
  349.  
  350. /* Virtual addresses of bounds of the two areas of memory in the core file.  */
  351.  
  352. extern CORE_ADDR data_start;
  353. extern CORE_ADDR data_end;
  354. extern CORE_ADDR stack_start;
  355. extern CORE_ADDR stack_end;
  356.  
  357. /* Virtual addresses of bounds of two areas of memory in the exec file.
  358.    Note that the data area in the exec file is used only when there is no core file.  */
  359.  
  360. extern CORE_ADDR text_start;
  361. extern CORE_ADDR text_end;
  362.  
  363. extern CORE_ADDR exec_data_start;
  364. extern CORE_ADDR exec_data_end;
  365.  
  366. /* Address in executable file of start of text area data.  */
  367.  
  368. extern int text_offset;
  369.  
  370. /* Address in executable file of start of data area data.  */
  371.  
  372. extern int exec_data_offset;
  373.  
  374. /* Address in core file of start of data area data.  */
  375.  
  376. extern int data_offset;
  377.  
  378. /* Address in core file of start of stack area data.  */
  379.  
  380. extern int stack_offset;
  381.  
  382. #ifdef COFF_FORMAT
  383. /* various coff data structures */
  384.  
  385. extern FILHDR file_hdr;
  386. extern SCNHDR text_hdr;
  387. extern SCNHDR data_hdr;
  388.  
  389. #endif /* not COFF_FORMAT */
  390.  
  391. /* a.out header saved in core file.  */
  392.   
  393. extern AOUTHDR core_aouthdr;
  394.  
  395. /* a.out header of exec file.  */
  396.  
  397. extern AOUTHDR exec_aouthdr;
  398.  
  399. extern void validate_files ();
  400.  
  401. core_file_command (filename, from_tty)
  402.      char *filename;
  403.      int from_tty;
  404. {
  405.   int val;
  406.   extern char registers[];
  407.  
  408.   /* Discard all vestiges of any previous core file
  409.      and mark data and stack spaces as empty.  */
  410.  
  411.   if (corefile)
  412.     free (corefile);
  413.   corefile = 0;
  414.  
  415.   if (corechan >= 0)
  416.     close (corechan);
  417.   corechan = -1;
  418.  
  419.   data_start = 0;
  420.   data_end = 0;
  421.   stack_start = STACK_END_ADDR;
  422.   stack_end = STACK_END_ADDR;
  423.  
  424.   /* Now, if a new core file was specified, open it and digest it.  */
  425.  
  426.   if (filename)
  427.     {
  428.       filename = tilde_expand (filename);
  429.       make_cleanup (free, filename);
  430.       
  431.       if (have_inferior_p ())
  432.     error ("To look at a core file, you must kill the inferior with \"kill\".");
  433.       corechan = open (filename, O_RDONLY, 0);
  434.       if (corechan < 0)
  435.     perror_with_name (filename);
  436.       /* 4.2-style (and perhaps also sysV-style) core dump file.  */
  437.       {
  438.     struct user u;
  439.     int reg_offset;
  440.  
  441.     val = myread (corechan, &u, sizeof u);
  442.     if (val < 0)
  443.       perror_with_name (filename);
  444.     data_start = exec_data_start;
  445.  
  446.     data_end = data_start + NBPG * u.u_dsize;
  447.     stack_start = stack_end - NBPG * u.u_ssize;
  448.     data_offset = NBPG * UPAGES;
  449.     stack_offset = NBPG * (UPAGES + u.u_dsize);
  450.     reg_offset = (int) u.u_ar0 - KERNEL_U_ADDR;
  451.  
  452.     /* I don't know where to find this info.
  453.        So, for now, mark it as not available.  */
  454.     core_aouthdr.a_magic = 0;
  455.  
  456.     /* Read the register values out of the core file and store
  457.        them where `read_register' will find them.  */
  458.  
  459.     {
  460.       register int regno;
  461.  
  462.       for (regno = 0; regno < NUM_REGS; regno++)
  463.         {
  464.           char buf[MAX_REGISTER_RAW_SIZE];
  465.  
  466.           val = lseek (corechan, register_addr (regno, reg_offset), 0);
  467.           if (val < 0)
  468.         perror_with_name (filename);
  469.  
  470.            val = myread (corechan, buf, sizeof buf);
  471.           if (val < 0)
  472.         perror_with_name (filename);
  473.           supply_register (regno, buf);
  474.         }
  475.     }
  476.       }
  477.       if (filename[0] == '/')
  478.     corefile = savestring (filename, strlen (filename));
  479.       else
  480.     {
  481.       corefile = concat (current_directory, "/", filename);
  482.     }
  483.  
  484.       set_current_frame ( create_new_frame (read_register (FP_REGNUM),
  485.                         read_pc ()));
  486.       select_frame (get_current_frame (), 0);
  487.       validate_files ();
  488.     }
  489.   else if (from_tty)
  490.     printf ("No core file now.\n");
  491. }
  492.  
  493. exec_file_command (filename, from_tty)
  494.      char *filename;
  495.      int from_tty;
  496. {
  497.   int val;
  498.  
  499.   /* Eliminate all traces of old exec file.
  500.      Mark text segment as empty.  */
  501.  
  502.   if (execfile)
  503.     free (execfile);
  504.   execfile = 0;
  505.   data_start = 0;
  506.   data_end -= exec_data_start;
  507.   text_start = 0;
  508.   text_end = 0;
  509.   exec_data_start = 0;
  510.   exec_data_end = 0;
  511.   if (execchan >= 0)
  512.     close (execchan);
  513.   execchan = -1;
  514.  
  515.   /* Now open and digest the file the user requested, if any.  */
  516.  
  517.   if (filename)
  518.     {
  519.       filename = tilde_expand (filename);
  520.       make_cleanup (free, filename);
  521.       
  522.       execchan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
  523.             &execfile);
  524.       if (execchan < 0)
  525.     perror_with_name (filename);
  526.  
  527. #ifdef COFF_FORMAT
  528.       {
  529.     int aout_hdrsize;
  530.     int num_sections;
  531.  
  532.     if (read_file_hdr (execchan, &file_hdr) < 0)
  533.       error ("\"%s\": not in executable format.", execfile);
  534.  
  535.     aout_hdrsize = file_hdr.f_opthdr;
  536.     num_sections = file_hdr.f_nscns;
  537.  
  538.     if (read_aout_hdr (execchan, &exec_aouthdr, aout_hdrsize) < 0)
  539.       error ("\"%s\": can't read optional aouthdr", execfile);
  540.  
  541.     if (read_section_hdr (execchan, _TEXT, &text_hdr, num_sections,
  542.                   aout_hdrsize) < 0)
  543.       error ("\"%s\": can't read text section header", execfile);
  544.  
  545.     if (read_section_hdr (execchan, _DATA, &data_hdr, num_sections,
  546.                   aout_hdrsize) < 0)
  547.       error ("\"%s\": can't read data section header", execfile);
  548.  
  549.     text_start = exec_aouthdr.text_start;
  550.     text_end = text_start + exec_aouthdr.tsize;
  551.     text_offset = text_hdr.s_scnptr;
  552.     exec_data_start = exec_aouthdr.data_start;
  553.     exec_data_end = exec_data_start + exec_aouthdr.dsize;
  554.     exec_data_offset = data_hdr.s_scnptr;
  555.     data_start = exec_data_start;
  556.     data_end += exec_data_start;
  557.     exec_mtime = file_hdr.f_timdat;
  558.       }
  559. #else /* not COFF_FORMAT */
  560.       {
  561.     struct stat st_exec;
  562.  
  563.     val = myread (execchan, &exec_aouthdr, sizeof (AOUTHDR));
  564.  
  565.     if (val < 0)
  566.       perror_with_name (filename);
  567.  
  568.         text_start = N_TXTADDR (exec_aouthdr);
  569.         exec_data_start = N_DATADDR (exec_aouthdr);
  570.  
  571.     text_offset = N_TXTOFF (exec_aouthdr);
  572.     exec_data_offset = N_TXTOFF (exec_aouthdr) + exec_aouthdr.a_text;
  573.  
  574.     text_end = text_start + exec_aouthdr.a_text;
  575.         exec_data_end = exec_data_start + exec_aouthdr.a_data;
  576.     data_start = exec_data_start;
  577.     data_end += exec_data_start;
  578.  
  579.     fstat (execchan, &st_exec);
  580.     exec_mtime = st_exec.st_mtime;
  581.       }
  582. #endif /* not COFF_FORMAT */
  583.  
  584.       validate_files ();
  585.     }
  586.   else if (from_tty)
  587.     printf ("No exec file now.\n");
  588.  
  589.   /* Tell display code (if any) about the changed file name.  */
  590.   if (exec_file_display_hook)
  591.     (*exec_file_display_hook) (filename);
  592. }
  593.  
  594. #ifdef __GNUC__
  595. /* Bad implement execle(3). It's depend for "/bin/cc".
  596.  
  597.    main()
  598.    {
  599.      printf("execle:\n");
  600.      execle(FILE, ARGS, envp);
  601.      exit(1);
  602.    }
  603.  
  604.    GCC:
  605.    link a6,#0
  606.    pea LC5    ; call printf
  607.    jbsr _printf
  608.    ;        ; (not popd stack)
  609.    pea _envp    ; call execle
  610.    clrl sp@-
  611.    pea LC4
  612.    pea LC4
  613.    pea LC4
  614.    pea LC3
  615.    pea LC6
  616.    jbsr _execle
  617.    addw #32,sp    ; delayed pop !!
  618.  
  619.    /bin/cc:
  620.    link.l    fp,#L23
  621.    movem.l    #L24,(sp)
  622.    pea    L26        ; call printf
  623.    jbsr    _printf
  624.    addq.l    #4,sp    ; <--- popd stack !!
  625.    pea    _envp        ; call execle
  626.    clr.l    -(sp)
  627.    pea    L32
  628.    
  629.    */
  630.  
  631. execle(name, args)
  632.      char *name, *args;
  633. {
  634.   register char    **env = &args;
  635.   while (*env++)
  636.     ;
  637.   execve(name, (char **)&args, (char **)*env);
  638. }
  639. #endif
  640.